package com.cnxdc.demo.service.impl;

import com.cnxdc.demo.mapper.CategoryMapper;
import com.cnxdc.demo.service.CategoryService;
import com.cnxdc.demo.utils.ThreadLocalUtil;
import com.cnxdc.demo.vo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int add(Category category) {
        //注意： 拿到用户的id，因为：类别属于某个用户!
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        category.setCreateUser(id);//将id设置到类别上
        //创建时间和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        int count = categoryMapper.add(category);
        return count;
    }

    @Override
    public List<Category> getall() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        List<Category> all=categoryMapper.getall(id);
        return all;
    }

    @Override
    public Category detail(Integer id) {
        //根据类别id查询类别
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");
        return categoryMapper.detail(id,userid);
    }

    @Override
    public void update(Category category) {
        // 注意数据权限!!!
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");
        category.setCreateUser(userid);
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> axios() {
        return categoryMapper.axios();
    }

    @Override
    public void delete(Integer id) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");

        categoryMapper.delete(id,userid);
    }

}
